import java.util.Arrays;

public class Solution435 {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals,(a,b)->a[1]-b[1]==0?b[0]-a[0]:a[1]-b[1]);
        int first=-1000000;
        int ans=0;
        for (int[] interval : intervals) {
            if (interval[0]>=first){
                first=interval[1];
                ans++;
            }
        }
        return intervals.length-ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution435().eraseOverlapIntervals(new int[][]{{1,2},{2,3},{3,4},{1,3}}));
    }
}
